<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        #app {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f2f2f2;
        }

        input[type="number"] {
            width: 50px;
            padding: 5px;
            text-align: center;
        }

        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 6px 12px;
            cursor: pointer;
            border-radius: 4px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .total-row {
            font-weight: bold;
        }

        span {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #999;
        }

        a {
            color: #007bff;
            cursor: pointer;
        }

        a:hover {
            text-decoration: underline;
        }

        .cart-summary {
            margin-bottom: 10px;
        }
    </style>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="app">
        <h1>{{msg}}</h1>
        
        <table v-if="cart.length != 0 || showMessage">
            <div class="cart-summary" v-if="cart.length != 0">
                总共{{cart.length}}个商品
            </div>
            <tr v-if="cart.length != 0">
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品数量</th>
                <th>商品小计</th>
                <th>操作</th>
            </tr>
            <tr v-for="item in cart" :key="item.id" v-if="cart.length != 0">
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <input type="number" v-model.number="item.count" min="1" @change="updateSubtotal(item)">
                </td>
                <td>{{item.price * item.count}}</td>
                <td><button @click="del(item.id)">移除</button></td>
            </tr>
            <tr class="total-row" v-if="cart.length != 0">
                <td colspan="3"></td>
                <td>总价：</td>
                <td>{{ total }}</td>
            </tr>
            <tr v-if="showMessage">
                <th>商品名称</th>
                <th>商品价格</th>
                <th>操作</th>
            </tr>
            <tr v-for="item in list" :key="item.id" v-if="showMessage">
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td><button @click="add(item)">加入购物车</button></td>
            </tr>
            <tr v-if="showMessage">
                <td colspan="3">
                    <button @click="closeMessage" style="width: 100%;">关闭商品列表</button>
                </td>
            </tr>
        </table>
        <span v-if="cart.length == 0 && !showMessage">
            购物车中暂无数据，<a @click="openMessage">请购物</a>
        </span>
    </div>
</body>
</html>
<script>
new Vue({
    el: '#app',
    data: {
        msg: '购物车',
        cart: [],
        list: [
            {'id': '1', 'name': '苹果', 'price': 8},
            {'id': '2', 'name': '哈密瓜', 'price': 18},
            {'id': '3', 'name': '老村长', 'price': 25},
        ],
        showMessage: false
    },
    methods: {
        del(id) {
            this.cart = this.cart.filter(item => item.id != id);
            if(this.cart.length == 0) {
                this.showMessage = false;
            }
        },
        add(item) {
            let existingItem = this.cart.find(cartItem => cartItem.id === item.id);
            if (existingItem) {
                existingItem.count += 1;
            } else {
                this.cart.push({ ...item, count: 1 });
            }
        },
        openMessage() {
            this.showMessage = !this.showMessage;
        },
        updateSubtotal(item) {
            if (item.count < 1) {
                item.count = 1; // 最小数量为1
            }
        },
        closeMessage() {
            this.showMessage = false;
        }
    },
    computed: {
        total() {
            return this.cart.reduce((total, item) => total + (item.price * item.count), 0);
        }
    }
});
</script>